package binaryTreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class LevelOrder {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(3);
        root.left = new TreeNode(9);
        root.right = new TreeNode(20);
        root.right.left = new TreeNode(15);
        root.right.right = new TreeNode(7);
        levelOrder(root);
    }
    public static List<List<Integer>> levelOrder(TreeNode root) {
        if(root == null)
            return null;
        List<List<Integer>> list =new ArrayList<>();
        // 初始化队列
        TreeNode cur = null;
        depth(list, root,0);
        System.out.println(list);
        return list;
    }
    public static  void  depth(List<List<Integer>> list, TreeNode root, int depth){
        if(root == null)
            return;
        // 判断当前层数是否为空，为空则添加一个空list
        if(list.size() == depth){
            list.add(new ArrayList<>());
        }
        list.get(depth).add(root.val);
        depth(list, root.left, depth+1);
        depth(list, root.right, depth+1);
    }
}
